<template>
    <div id="clinicAllEquip">
        <header>
            <i @click="goBack()"></i>
            <div>诊室设备信息</div>
            <span @click="showMcFun(1)">添加</span>
        </header>
        <div class="border-8"></div>
        <ul class="list">
            <li>
                <b>
                    我的设备:
                </b>
            </li>
            <div class='mc2' v-if="list.length<1">
                <!-- <img src="../../image/no_collection@2x.png" width='110' alt=""> -->
                <p>暂无设备!</p>
            </div>
            <li v-for="item in list">
                <router-link :to="{name: 'clinicEquip',query: {id: item.id,chairname: item.numbering}}">
                    {{item.numbering}}
                </router-link>
            </li>
        </ul>
        <div class="mc" v-show="showMc" @click.stop="showMcFun(0)">
            <ul @click.stop="showMcFun(1)">
                <li>
                    <span>椅子编号:</span>
                    <input type="text" placeholder="请输入椅位编号" v-model="chairObj.numbering">
                </li>
                <li>
                    <span>计时器编号:</span>
                    <input type="text" placeholder="请输入计时器编号" v-model="chairObj.timing_number">
                </li>
                <li>
                    <a @click.stop="subForm">确定</a>
                </li>
            </ul>
        </div>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                equipNum: 3,
                list: [], //椅子列表
                showMc: false,
                chairObj: {
                    clinicid: '',       //诊室id
                    numbering: '',  //添加椅子编号
                },
            }
        },
        components: {
            
        },
        methods: {
            goBack() {
                this.$router.push({name:'home'});
            }, 
            // 控制添加窗口显示隐藏
            showMcFun(status) { //0隐藏
                if(status) {
                    this.showMc = true;
                } else {
                    this.showMc = false;
                }
                
            },
            // 添加椅子
            addTimer() {
                this.$api.post(this.$api.baseURLSchool,"updatedata/chairs/add",this.chairObj,r => {
                    if(r.data.ret == 1) {
                        this.$utils._toast("添加成功");
                        this.getList();
                        this.showMcFun(0);
                    }
                },err => {
                    console.log(err);
                })
            },
            // 查看该编号是否绑定有椅子
            isHasChair(number) {
                this.$api.get(this.$api.baseURLSchool,"selectdata2/chairs?timing_number="+number,{},r => {
                    if(r.data.ret == 1) {
                        if(r.data.info.length > 0) {
                            this.$utils._toast("该计时器已经绑定设备");
                        }
                    }else if(r.data.ret== 0) {
                            this.addTimer();
                    }
				},err => {  
					console.log(err);
				})
            },
            // 确定添加椅子
            subForm() {
                if(this.chairObj.numbering&&this.chairObj.timing_number) {
                    // 查询计时器编号是否存在
                    this.$api.get(this.$api.baseURLSchool,"select_cid/timer/"+this.chairObj.timing_number,{},r => {
                        if(r.data.ret == 1) {
                            //编号存在
                            // 查看是否绑定椅子
                            this.isHasChair(this.chairObj.timing_number);
                        } else {
                            this.$utils._toast("编号不存在");
                        }
                    },err => {
                        console.log(err);
                    })
                } else {
                    this.$utils._toast("请输入椅子编号及计时器编号");
                }
                
            },
            // 获取椅子列表
            getList() {
                this.chairObj.clinicid = localStorage.getItem("loginMobile");
                this.$api.get(this.$api.baseURLSchool,"selectdata2/chairs?clinicid="+this.chairObj.clinicid,{},r => {
                    if(r.data.ret == 1) {
                        this.list = r.data.info;
                    }
                },err => {  
                    console.log(err);
                })
            }, 
        },
        created() {
            this.getList();
        },
        mounted() {
        }
    }
</script>
<style lang="scss" scoped>
    .mc2 {
        padding-top: 40px;
        img {
            display: block;
            margin: 0 auto;
        }
        p {
            padding-top: 10px;
            text-align: center;
        }
    }
</style>
      